//turret.txt - Simple script for turrets
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;
short last_spawn;
short spawn_mes = 0;
short final_spawn = 0;
short talked = 0;
short r1;

body;

beginstate INIT_STATE;
	set_name(ME,"Ghesss");
	change_max_health(ME,200);
	set_boss_level(ME,2);
	set_walk_speed(ME,0);
	last_spawn = get_current_tick();
	break;

beginstate DEAD_STATE;
	sf(26,6,1);
	begin_talk_mode(36);
break;

beginstate START_STATE; 
	if (who_shot_me() >= 0) {
		if (talked == 0) {
			talked = 1;
			talk_no_exit(10);
			}

		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

		
	if (get_foe_target(ME,8,0)) {
		if (talked == 0) {
			talked = 1;
			talk_no_exit(10);
			}

		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking

	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((get_health(ME) < get_max_health(ME) / 3) && (final_spawn == 0)) {
		final_spawn = 1;
		i = 63;
		while (i <= 68) {
			if (char_ok(i) == FALSE) {
				spawn_creature(i - 8);
				set_summon_level(i,1);
				place_particle_num(i,10,7,10);
				}
			i = i + 1;
			}
		run_char_animation(2,1,50);	
		pc_heard_sound_delay(136,100);						
		begin_talk_mode(35);
		}
		
	if (((target_ok()) || (who_shot_me() >= 0)) && (final_spawn == 0)) {
		if ((tick_difference(last_spawn,get_current_tick()) > 1) && (get_attitude(ME) >= 10)) {
			r1 = get_ran(1,0,5);
			if (char_ok(63 + r1))
				r1 = get_ran(1,0,5);
				
			if (char_ok(63 + r1) == FALSE) {
				if (spawn_creature(55 + r1)) {
					print_str_color("Ghesss calls forth a new shade! It slowly takes form on the platform.",3);
					set_summon_level(63 + r1,1);
					place_particle_num(63 + r1,10,7,10);
					run_char_animation(2,1,50);	
					pc_heard_sound_delay(136,100);						
					
					last_spawn = get_current_tick();

					end();
					}
				}
			}
		}

	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
	print_str("All it really is is a big fungus. It can't communicate at all.");
break;